java聊天软件,现在要连接一个MySQL数据库,功能是登陆验证,和调出聊天记录
Eclipse环境下,MySQL数据库,mysql-connector-java-5.1.7驱动,求连接代码!!!!...
Eclipse环境下,MySQL数据库,mysql-connector-java-5.1.7驱动,求连接代码!!!!
展开
3个回答
展开全部
import java.sql.*;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.*;
public class try_0822 {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn; conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/tw","root","123");
System.out.println("数据库链接成功");
PreparedStatement statl;
String sql="insert into tt values (?,?)";
statl=conn.prepareStatement(sql);
statl.setString(1,"fd");
statl.setString(2,"fdf");
int x=statl.executeUpdate();
}
catch(SQLException SQLe){
System.out.println("数据库错误!");
}
catch(Exception h){}
// TODO, add your application code
// System.out.println("Hello World!");
}
}
上面的123是你自己的密码,我这里有个表叫做tt,根据自己的改变吧
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.*;
public class try_0822 {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn; conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/tw","root","123");
System.out.println("数据库链接成功");
PreparedStatement statl;
String sql="insert into tt values (?,?)";
statl=conn.prepareStatement(sql);
statl.setString(1,"fd");
statl.setString(2,"fdf");
int x=statl.executeUpdate();
}
catch(SQLException SQLe){
System.out.println("数据库错误!");
}
catch(Exception h){}
// TODO, add your application code
// System.out.println("Hello World!");
}
}
上面的123是你自己的密码,我这里有个表叫做tt,根据自己的改变吧
展开全部
oracle 连接, MySQL类似,应该可以解决
/**
* @author jiangzongshuai
*
*/
public class OraSqlConnection {
private static String className = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static String username = "scott";
private static String password = "tiger";
private static Connection conn = null;
public static Connection getConnection() {
try {
if (conn !=null) return conn;
Class.forName(className);
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
/**
* @author jiangzongshuai
*
*/
public class OraSqlConnection {
private static String className = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static String username = "scott";
private static String password = "tiger";
private static Connection conn = null;
public static Connection getConnection() {
try {
if (conn !=null) return conn;
Class.forName(className);
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-04-09
展开全部
这个东西是很简单的呀,
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1/0901student", "root", "deng");
connection.setAutoCommit(false);
//...................解决此离线操作超成的异常。
CachedRowSet jrs = new CachedRowSetImpl();
jrs.setUsername("root");
jrs.setPassword("deng");
jrs.setUrl("jdbc:mysql://127.0.0.1/0901student");
jrs.setCommand("select * from studentes");
jrs.execute();
//CachedRowSet jrs2 = new CachedRowSetImpl();
//jrs2.populate(jrs);
//调用populate(ResultSet)
jrs.absolute(1);
jrs.updateString(3, "男");
jrs.acceptChanges(connection);
jrs.next();
jrs.updateString(4,"my");
jrs.updateRow();//cacheRowSetImpl是离线操作,是与内存打交道的,它先更新缓存区德数据,
//数据库接受我的数据更新,使得缓冲区德数据与数据库数据同步更新
//..快速定位到某行,修改数据
/*jrs.absolute(7);
jrs.moveToInsertRow();
jrs.updateString("name", "dengliang");
jrs.updateInt("id", 10);
jrs.updateInt("age", 56);
jrs.updateString("sex", "邓亮");
jrs.insertRow();
jrs.moveToCurrentRow();
jrs.acceptChanges();*/
//快速定位到插入一条记录
/* jrs.absolute(6);
jrs.moveToCurrentRow();
jrs.deleteRow();
jrs.moveToCurrentRow();
jrs.acceptChanges();*/
//快速定位到某行删除此行
/*jrs.setPageSize(2);
jrs.execute()*/;
//分页技术
//while(jrs.nextPage()){//访问下一页数据块,所谓的下一页数据块,就是除了分页的几条记录的下页的记录
while (jrs.next()) {
System.out.println(jrs.getInt("id")+" "+jrs.getString("name")+" "+jrs.getInt("age")+" "+jrs.getString("sex"));
//
}
}
}
//}
就这点呀,嘿嘿,你是高级软件工程师,现在在华为上班,qq:646282142
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1/0901student", "root", "deng");
connection.setAutoCommit(false);
//...................解决此离线操作超成的异常。
CachedRowSet jrs = new CachedRowSetImpl();
jrs.setUsername("root");
jrs.setPassword("deng");
jrs.setUrl("jdbc:mysql://127.0.0.1/0901student");
jrs.setCommand("select * from studentes");
jrs.execute();
//CachedRowSet jrs2 = new CachedRowSetImpl();
//jrs2.populate(jrs);
//调用populate(ResultSet)
jrs.absolute(1);
jrs.updateString(3, "男");
jrs.acceptChanges(connection);
jrs.next();
jrs.updateString(4,"my");
jrs.updateRow();//cacheRowSetImpl是离线操作,是与内存打交道的,它先更新缓存区德数据,
//数据库接受我的数据更新,使得缓冲区德数据与数据库数据同步更新
//..快速定位到某行,修改数据
/*jrs.absolute(7);
jrs.moveToInsertRow();
jrs.updateString("name", "dengliang");
jrs.updateInt("id", 10);
jrs.updateInt("age", 56);
jrs.updateString("sex", "邓亮");
jrs.insertRow();
jrs.moveToCurrentRow();
jrs.acceptChanges();*/
//快速定位到插入一条记录
/* jrs.absolute(6);
jrs.moveToCurrentRow();
jrs.deleteRow();
jrs.moveToCurrentRow();
jrs.acceptChanges();*/
//快速定位到某行删除此行
/*jrs.setPageSize(2);
jrs.execute()*/;
//分页技术
//while(jrs.nextPage()){//访问下一页数据块,所谓的下一页数据块,就是除了分页的几条记录的下页的记录
while (jrs.next()) {
System.out.println(jrs.getInt("id")+" "+jrs.getString("name")+" "+jrs.getInt("age")+" "+jrs.getString("sex"));
//
}
}
}
//}
就这点呀,嘿嘿,你是高级软件工程师,现在在华为上班,qq:646282142
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询